package com.itrident.typehandler;

import com.intellij.codeInsight.AutoPopupController;
import com.intellij.codeInsight.completion.CompletionPhase;
import com.intellij.codeInsight.completion.impl.CompletionServiceImpl;
import com.intellij.codeInsight.editorActions.CompletionAutoPopupHandler;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

/**
 * @author cxx
 * @create 2021-04-20 22:19
 **/
public class XmlCompletionAutoPopupHandler extends CompletionAutoPopupHandler {
    @Override
    public Result checkAutoPopup(char charTyped, @NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file) {

        CompletionPhase phase = CompletionServiceImpl.getCompletionPhase ();
        if ( charTyped == '@') {
            if (phase instanceof CompletionPhase.EmptyAutoPopup && ((CompletionPhase.EmptyAutoPopup)phase).allowsSkippingNewAutoPopup(editor, charTyped)) {
                return Result.CONTINUE;
            }
            AutoPopupController.getInstance (project).scheduleAutoPopup (editor);
            return Result.STOP;
        }
        return Result.CONTINUE;
    }

    @Override
    public @NotNull Result charTyped(char c, @NotNull Project project, @NotNull Editor editor, @NotNull PsiFile file) {
        if ( c == '@') {
            return Result.STOP;
        }
        return super.charTyped (c, project, editor, file);
    }
}